package xyz.zcxsythenew.msec

import android.content.Context
import android.os.Bundle
import android.text.Editable
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import com.google.android.material.snackbar.Snackbar
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder

/**
 * A simple [Fragment] subclass as the second destination in the navigation.
 */
class SecondFragment : Fragment() {

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
        val inputId = sharedPref?.getString("id", "")
        val inputPassword = sharedPref?.getString("password", "")
        view.findViewById<EditText>(R.id.edittext_login_id).setText(inputId)
        view.findViewById<EditText>(R.id.edittext_login_password).setText(inputPassword)

        view.findViewById<Button>(R.id.button_second).setOnClickListener {
            val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
            imm?.hideSoftInputFromWindow(view.windowToken, 0)
            Thread(Runnable {
                val name = view.findViewById<EditText>(R.id.edittext_login_id).text.toString()
                val password = view.findViewById<EditText>(R.id.edittext_login_password).text.toString()
                val queryName = URLEncoder.encode(name, "UTF-8")
                val queryPassword = URLEncoder.encode(password, "UTF-8")
                val pref = activity?.getPreferences(Context.MODE_PRIVATE)
                with(pref?.edit()) {
                    this?.putString("id", queryName)
                    this?.putString("password", queryPassword)
                    this?.commit()
                }
                val url = URL("http://192.168.1.10:7001/login?id=$queryName&password=$queryPassword")
                val connection = url.openConnection() as HttpURLConnection
                connection.run {
                    requestMethod = "GET"
                    connect()
                    val responseMessage = when (responseCode) {
                        HttpURLConnection.HTTP_NO_CONTENT -> "Success"
                        HttpURLConnection.HTTP_UNAUTHORIZED -> "Password mismatch"
                        HttpURLConnection.HTTP_NOT_FOUND -> "User not found"
                        else -> "Fatal error"
                    }
                    activity?.runOnUiThread {
                        Snackbar.make(it, responseMessage, Snackbar.LENGTH_LONG).show()
                    }
                    disconnect()
                }
            }).start()
        }
    }
}